// 引入必要的OpenLayers和GeoJSON模块
import Feature from "ol/Feature";
import GeoJSON from "ol/format/GeoJSON"
import VectorSource from 'ol/source/Vector'
import { Vector as VectorLayer } from "ol/layer";
import { Fill, Text, Stroke, Style } from "ol/style";

/**
 * 用于处理边界线和注记
 */

// 用于处理GeoJSON 数组的加载和显示
export default class olGeoJson_bordLine_annotation {
    // 边界线基本特征属性
    private static boundar = { Width: 1, Color: "blue", fillColor: "rgba(255,255,255,0.2)" }

    // 注记基本特征属性
    private static annotation = { font: "16px Arial", font_color: "#000", font_bord_color: "#fff", font_bord_width: 2 }

    // 创建一个用于显示边界线的VectorLayer
    public createBoundaryLayer(geoJsonArray: Array<any>,layerId:string,levelOptions?:{minLevel?:number,maxLevel?:number}): VectorLayer {
        // debugger
        const vectorSource = new VectorSource();
        // 遍历GeoJSON数组，将每个GeoJSON对象转换为Feature并添加到VectorSource中
        geoJsonArray.forEach((geoJson: GeoJSON.GeoJsonObject) => {
            const features = new GeoJSON().readFeatures(geoJson, {
                featureProjection: "EPSG:3857", // 目标投影是WEB Mercator
                dataProjection: "EPSG:4326",  // 源数据投影是WGS 84

            })
            features.forEach((feature:Feature)=>{
                feature.setStyle(new Style({
                    stroke: new Stroke({
                        color: olGeoJson_bordLine_annotation.boundar.Color, // 边界线颜色
                        width: olGeoJson_bordLine_annotation.boundar.Width   // 边界线宽度
                    }),
                    fill: new Fill({
                        color: olGeoJson_bordLine_annotation.boundar.fillColor // 填充颜色及透明度
                    })
                }))
         
            })
            vectorSource.addFeatures(features)
        })

        const boundaryLayer = new VectorLayer({
            source: vectorSource,
            zIndex:3
        })
        if(levelOptions && levelOptions.maxLevel!==undefined){
            boundaryLayer.setMaxZoom(levelOptions.maxLevel)
        }
        if(levelOptions && levelOptions.minLevel!==undefined){
            boundaryLayer.setMinZoom(levelOptions.minLevel)
        }
      

        boundaryLayer.set("id",layerId)
        return boundaryLayer;
    }

    // 创建一个用于显示注记的VerctorLayer
    // level {minlevel,maxlevel}
    public createLabelLayer(geoJsonArray: Array<any>,layerId:string,levelOptions?:{minLevel?:number,maxLevel?:number}): VectorLayer {
        const vectorSource = new VectorSource();
        // 遍历GeoJSON数组，将每个GeoJSON对象转换为Feature并添加到VectorSource中
        geoJsonArray.forEach((geoJson: GeoJSON.GeoJsonObject) => {
            const features = new GeoJSON().readFeatures(geoJson, {
                featureProjection: "EPSG:3857", // 目标投影是WEB Mercator
                dataProjection: "EPSG:4326",  // 源数据投影是WGS 84
            

            })
            features.forEach((feature:Feature)=>{
              const properties = feature.getProperties()
              feature.setStyle(new Style({
                text: new Text({
                    font: olGeoJson_bordLine_annotation.annotation.font,// 注记字体
                    text: properties.name, // 根据Feature的属性设置注记内容，假设为name
                    fill: new Fill({
                        color: olGeoJson_bordLine_annotation.annotation.font_color // 注记颜色
                    }),
                    stroke: new Stroke({
                        color: olGeoJson_bordLine_annotation.annotation.font_bord_color,// 注记描边颜色
                        width: olGeoJson_bordLine_annotation.annotation.font_bord_width // 注记描边宽度
                    }),
                    offsetX:-10 // 注记垂直偏移量，使注记不覆盖在边界线上
                })
              }))
            })
            vectorSource.addFeatures(features)
        })

        const labelLayer = new VectorLayer({
            source: vectorSource,
            zIndex:3
        })
        if(levelOptions && levelOptions.maxLevel!==undefined){
            labelLayer.setMaxZoom(levelOptions.maxLevel)
        }
        if(levelOptions && levelOptions.minLevel!==undefined){
            labelLayer.setMinZoom(levelOptions.minLevel)
        }
        labelLayer.set("id",layerId)
        return labelLayer
    }
}

